//author:kakake
class tsfrontDao {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    constructor(workid: number) {
        this.data.WorkId = workid;
    }

    /**
     * 获取商户号配置
     */
    getMch(SubMchId): any {
        var strsql = "select * from Pay_Mch where SubMchId=@SubMchId";
        var mch = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { SubMchId: SubMchId });
        if (mch == null) {
            throw '商户号配置错误';
        }

        return mch;
    }
    /**
     * 获取支付方式配置
     */
    getPayment(SubMchId, PayType) {
        let strsql = "select * from Pay_PaymentType where SubMchId=@SubMchId and PayType=@PayType";
        let dbparams = {
            SubMchId: SubMchId,
            PayType: PayType
        }
        var payment = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (payment == null) {
            return { code: 1, msg: '配置错误' };
        }

        return payment;
    }
    /**
     * 获取订单
     */
    getOrder(SubMchId, BusinessOrderNo) {
        let strsql = "select * from Pay_Order where SubMchId=@SubMchId and BusinessOrderNo=@BusinessOrderNo";
        let dbparams = {
            SubMchId: SubMchId,
            BusinessOrderNo: BusinessOrderNo
        }
        var order = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return order;
    }

    /**
     * 根据订单号获取订单数据
     */
    getOrderByNo(orderNo) {

        var strsql = "select * from Pay_Order where OrderNo=@orderNo";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { orderNo: orderNo });
    }

    /**
     * 获取支付方式
     */
    getPayTypeByNo(orderNo) {
        var strsql = "select SubMchId from pay_order where orderNo=@orderNo";
        var SubMchId = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { orderNo: orderNo });

        if (!SubMchId)
            throw '找不到订单';

        strsql = "select * from pay_paymenttype where SubMchId=@SubMchId and IsUse=1"
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { SubMchId: JSON.parse(SubMchId) });
        return data;
    }


    getOrderStateByNo(orderNo) {
        var strsql = "select paystate from pay_order where orderNo=@orderNo";
        var paystate = TSLobsterAPI.DbHelper.DbExecuteQuery(this.data.dbConn, strsql, { orderNo: orderNo });
        if (!paystate) {
            throw '找不到订单'
        }

        return JSON.parse(paystate);
    }

    /**
     * 更新订单支付方式
     */
    updateOrderPayType(orderNo, payType) {
        let strsql = "update pay_order set paytype=@payType where orderno=@orderNo";

        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { orderNo: orderNo, payType: payType });
    }

    /**
     * 获取退款订单
     */
    getRefundOrder(SubMchId, BusinessRefundNo) {
        let strsql = "select * from Pay_RefundOrder where SubMchId=@SubMchId and BusinessRefundNo=@BusinessRefundNo";
        var refundorder = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { SubMchId: SubMchId, BusinessRefundNo: BusinessRefundNo });
        return refundorder;
    }

    getRefundOrderByNo(RefundNo) {
        var strsql = "select * from Pay_RefundOrder where RefundNo=@RefundNo";
        var refundorder = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { RefundNo: RefundNo });
        return refundorder;
    }

    getRefundOrderListByNo(orderNo){
        var  strsql = "select * from Pay_RefundOrder where OrderNo=@orderNo and RefundStatus=1";
        var refundorderlist= TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, { orderNo: orderNo });
        return refundorderlist;
    }
}